package com.example.satu.quizcreator.quiz_creator;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.satu.quizcreator.quiz.Question;
import com.example.satu.quizcreator.R;

import java.util.ArrayList;

/**
 * Created by Satu on 2014-07-21.
 */
public class QuestionAdapter extends ArrayAdapter<Question> {
    private final LayoutInflater mInflater;

    public QuestionAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
        mInflater = LayoutInflater.from(getContext());

    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.item_question, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.remove = (Button) convertView.findViewById(R.id.delete_question);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        final Question question = getItem(position);

        holder.remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                remove(question);
            }
        });

        if(question.getId() != 0 )
            holder.title.setText("Pytanie "+position);
        else{
            holder.title.setText("Dodaj nowe pytanie");
            holder.remove.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    private static class ViewHolder{
        public TextView title;
        public Button   remove;
    }
    public ArrayList<Question> getQuestions(){
        ArrayList<Question> questions = new ArrayList<Question>();
        for (int i = 1; i < getCount(); i++){
            questions.add(getItem(i));
        }
        return questions;
    }
}
